package persistence;

import junit.framework.Assert;
import junit.framework.TestCase;

/**
 * @author idan
 */
public abstract class PersistentTest extends TestCase
{
	public PersistentTest()
	{
		super() ;
		this.p = new Persistent[PersistentTest.TESTED_OBJECT_COUNT] ;
	}

	private static final int TESTED_OBJECT_COUNT = 3 ;
	Persistent[] p ;

	/**
	 * Test method for {@link persistence.Persistent#hashCode()}.
	 */
	public void testHashCode()
	{
		this.testEqualsObject() ;

		for (int i = 0 ; i < PersistentTest.TESTED_OBJECT_COUNT ; i++)
		{
			for (int j = 0 ; j < PersistentTest.TESTED_OBJECT_COUNT ; j++)
			{
				if (this.p[i].equals(this.p[j]))
				{
					Assert.assertTrue(this.p[i].hashCode() == this.p[j].hashCode()) ;
				}
			}
		}
	}

	protected abstract Persistent createPersistent(long id) ;

	/**
	 * Test method for {@link persistence.Persistent#equals(java.lang.Object)}.
	 */
	public void testEqualsObject()
	{
		Assert.assertTrue(this.p[0].equals(this.p[1])) ;
		Assert.assertTrue(this.p[1].equals(this.p[0])) ;
		Assert.assertFalse(this.p[0].equals(this.p[2])) ;
		Assert.assertFalse(this.p[2].equals(this.p[0])) ;
	}

	@Override
	protected void setUp() throws Exception
	{
		super.setUp() ;

		this.p[0] = this.createPersistent(1354) ;
		this.p[1] = this.createPersistent(1354) ;
		this.p[2] = this.createPersistent(245) ;
	}

}
